package com.tangyuan.niochat.protocol.serializer;

import com.alibaba.fastjson.JSON;
import com.tangyuan.niochat.protocol.Serializer;
import com.tangyuan.niochat.protocol.comman.SerializerAlgorithm;

/**
 * Created on 2019-08-28
 *  json序列化算法具体实现
 * @author fenghongyu
 */
public class JSONSerializer implements Serializer {
    @Override
    public byte getSerializerAlgorithm() {
        return SerializerAlgorithm.JSON;
    }

    /**
     * java对象转成二进制
     * @param object
     * @return
     */
    @Override
    public byte[] serializer(Object object) {
        return JSON.toJSONBytes(object);
    }

    /**
     * 二进制转成Java对象
     * @param clazz
     * @param bytes
     * @param <T>
     * @return
     */
    @Override
    public <T> T deserializer(Class<T> clazz, byte[] bytes) {
        return JSON.parseObject(bytes, clazz);
    }
}
